package cn.sccl.common.scheduler.controller;

import cn.sccl.common.scheduler.SchedulerService;
import cn.sccl.common.scheduler.model.TaskScheduled;
import cn.sccl.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author _minus
 * @create 2017-10-08 1:50
 */
@RestController
@RequestMapping(value = "/framework/scheduler/scheduled")
public class ScheduledController extends BaseController {


    @Autowired
    private SchedulerService schedulerService;

    /**
     * 新增任务
     *
     * @param scheduled
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public TaskScheduled updateTask(@RequestBody TaskScheduled scheduled) {

        schedulerService.updateTask(scheduled);
        return scheduled;
    }

    /**
     * 删除某一个任务
     *
     * @param scheduled
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public void delete(@RequestBody TaskScheduled scheduled) {
        Assert.notNull(scheduled.getTaskGroup(), "TASKGROUP");
        Assert.notNull(scheduled.getTaskName(), "TASKNAME");
        schedulerService.delTask(scheduled);
    }

    /**
     * 执行某一个任务
     *
     * @param scheduled
     * @return
     */
    @RequestMapping(value = "/exec", method = RequestMethod.POST)
    public TaskScheduled exec(@RequestBody TaskScheduled scheduled) {
        Assert.notNull(scheduled.getTaskGroup(), "TASKGROUP");
        Assert.notNull(scheduled.getTaskName(), "TASKNAME");
        schedulerService.execTask(scheduled);
        return scheduled;
    }

    /**
     * 恢复作业
     *
     * @param scheduled
     * @return
     */
    @RequestMapping(value = "/resumeJob", method = RequestMethod.POST)
    public TaskScheduled open(@RequestBody TaskScheduled scheduled) {
        Assert.notNull(scheduled.getTaskGroup(), "TASKGROUP");
        Assert.notNull(scheduled.getTaskName(), "TASKNAME");
        schedulerService.resumeJob(scheduled);
        return scheduled;
    }

    /**
     * 获取所有任务列表
     *
     * @return
     */
    @RequestMapping(value = "/all/tasks", method = RequestMethod.GET)
    public List<TaskScheduled> getAllTasks() {
        return schedulerService.getAllTaskDetail();
    }


    /**
     * 查询任务列表
     *
     * @param name
     * @param group
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public List<TaskScheduled> getTasksByGroup(@RequestParam(required = false) String name, @RequestParam String group) {
        List<TaskScheduled> all = schedulerService.getAllTaskDetail();
        List<TaskScheduled> result = new ArrayList<TaskScheduled>();

        for (TaskScheduled taskScheduled : all) {
            if (group != null) {
                if (taskScheduled.getTaskGroup().equals(group)) {
                    if (name != null) {
                        if (taskScheduled.getTaskName().toUpperCase().indexOf(name.toUpperCase()) >= 0) {
                            result.add(taskScheduled);
                        }
                    } else {
                        result.add(taskScheduled);
                    }
                }
            }
        }
        return result;

    }


    /**
     * 暂停job
     *
     * @param scheduled
     * @return
     */
    @RequestMapping(value = "/pauseJob", method = RequestMethod.POST)
    public TaskScheduled pauseJob(@RequestBody TaskScheduled scheduled) {
        Assert.notNull(scheduled.getTaskGroup(), "TASKGROUP");
        Assert.notNull(scheduled.getTaskName(), "TASKNAME");
        schedulerService.pauseJob(scheduled);
        return scheduled;
    }


}
